﻿/**
 * @copyright Copyright 2009 Structured Solutions
 * @license http://www.codeplex.com/ShippingAgent/license Microsoft Public License (Ms-PL)
 * @version 3.6
 */

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace StructuredSolutions.ShippingAgent
{
    public static class ShippingAgentHelper
    {
        public const string ORDER_EXPORT_TEXT = "Exported by Shipping Agent to {0}.";
        public const string ORDER_UPDATE_TEXT = "This order has been changed by Shipping Agent: {0}";

        public static bool ConvertToBoolean(object value)
        {
            return ConvertToBoolean(value, false);
        }

        public static bool ConvertToBoolean(object value, bool defaultValue)
        {
            if (value == null || value == Convert.DBNull)
                return defaultValue;
            return Convert.ToBoolean(value);
        }

        public static decimal ConvertToDecimal(object value)
        {
            if (value == null) return 0;
            return value == Convert.DBNull ? 0 : Convert.ToDecimal(value);
        }

        public static double ConvertToDouble(object value)
        {
            if (value == null) return 0;
            return value == Convert.DBNull ? 0 : Convert.ToDouble(value);
        }

        public static int ConvertToInt32(object value)
        {
            if (value == null) return 0;
            return value == Convert.DBNull ? 0 : Convert.ToInt32(value);
        }

        public static long ConvertToInt64(object value)
        {
            if (value == null) return 0;
            return value == Convert.DBNull ? 0 : Convert.ToInt64(value);
        }

        public static DateTime? ConvertToNullableDateTime(object value)
        {
            if (value == null) return null;
            return value == Convert.DBNull ? default(DateTime?) : Convert.ToDateTime(value);
        }

        public static string ConvertToString(object value)
        {
            if (value == null) return string.Empty;
            return value == Convert.DBNull ? string.Empty : Convert.ToString(value);
        }

        public static T? ConvertWebServiceNullable<T>(T value, bool valueSpecified) where T:struct
        {
            if (valueSpecified)
                return value;
            return null;
        }

        public static string RemoveHtmlTags(string value)
        {
            return Regex.Replace(value, @"</?(\w+)*|/?>", string.Empty).Replace("&nbsp;", " ").Replace("  ", " ");
        }

        public static string SplitCompoundName(string name)
        {
            if (string.IsNullOrEmpty(name))
                return null;

            StringBuilder builder = new StringBuilder();
            var isPhraseStart = true;
            var index = 0;
            foreach (var ch in name)
            {
                var isLetter = char.IsLetter(ch);
                var isDigit = char.IsDigit(ch);
                if (!isLetter && !isDigit)
                {
                    return name;
                }
                var isWordStart = (isLetter && char.IsUpper(ch)) || isDigit;
                if (isWordStart && !isPhraseStart)
                {
                    builder.Append(' ');
                }
                isPhraseStart = isWordStart || (((index == 0)) && char.IsLower(ch));
                builder.Append(ch);
                index++;
            }
            return builder.ToString();
        }

        public static string StringOrNull(object value)
        {
            if (value == null || value == Convert.DBNull)
                return null;

            var trimmed = value.ToString().Trim();

            return trimmed == string.Empty ? null : trimmed;
        }

        public static bool StringsAreEqual(string s1, string s2, StringComparison stringComparison)
        {
            var trimmed1 = StringOrNull(s1);
            var trimmed2 = StringOrNull(s2);
            if (trimmed1 == null && trimmed2 == null)
                return true;
            return string.Equals(s1, s2, stringComparison);
        }
    }
}
